package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/app/requestx"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreListLogic {
	return &StoreListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商家列表
func (l *StoreListLogic) StoreList(in *agentClient.StoreListReq) (*agentClient.StoreListResp, error) {
	count := l.svcCtx.StoreModel.FindListCount(l.ctx, in.Keyword, in.Platform, in.Status, in.AgentId, in.SelectFirstPass)

	res := make([]*agentClient.Store, 0)
	if count > 0 {
		list, err := l.svcCtx.StoreModel.FindList(l.ctx, &requestx.ListReq{
			Page:     in.Page,
			PageSize: in.PageSize,
			Keyword:  in.Keyword,
		}, in.Platform, in.Status, in.AgentId, in.SelectFirstPass)
		if err != nil {
			return nil, err
		}
		for _, store := range list {
			temp := &agentClient.Store{}
			copier.Copy(temp, &store)
			temp.CreateTime = store.CreateTime.Unix()
			temp.UpdateTime = store.UpdateTime.Unix()
			res = append(res, temp)
		}
	}

	return &agentClient.StoreListResp{
		List:  res,
		Total: count,
	}, nil
}
